<?php
namespace Home\Controller;
use Think\Controller;
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class AjaxController extends Controller {
    
    /*
     * 获取数据库字段
     */
    function getFieldList($tablename=""){
        $table=D($tablename);
        $fields =  $table->getDbFields();
        $fieldsList=array();
        for($i=0;$i<count($fields);$i++){
            $fieldsList[$i]['name']=$fields[$i];
            $fieldsList[$i]['checked']=FALSE;
        }
        $result=array('code'=>0,'msg'=>'ok','tablename'=>$tablename,'fields'=>$fieldsList);
        $this->ajaxReturn($result,'JSON');
    }
    
    /*
     * 返回带处理的数据
     */
    function getQueryFieldList($fieldListstr=""){
        //dump($fieldListstr);
        $reqstr="";
        $fieldArray=explode(',',$fieldListstr);
        for($i=0;$i<count($fieldArray);$i++){
            $reqstr= $reqstr."$$fieldArray[$i]='',";
        };
        $reqstr=rtrim($reqstr, ",");
        $result=array('code'=>0,'msg'=>'ok','RequestStr'=>$reqstr);
        $this->ajaxReturn($result);
    }
    
    /*
     * 返回CURD ADD 代码
     */
    function getCURDAddCode($tablename="",$fieldListstr=""){
        $fieldArray=explode(',',$fieldListstr);
         $reqstr="\n";
        for($i=0;$i<count($fieldArray);$i++){
            $reqstr= $reqstr."\t'$fieldArray[$i]'=>$$fieldArray[$i],\n";
        };
        $reqstr=rtrim($reqstr, ",");
        
        $codes="\n$$tablename = array($reqstr); \n".
            "$"."id = M('$tablename')->add($$tablename);\n".
            "if (!$"."id) {\n".
            "\t$"."this->error('添加失败！');\n".
            "} else {\n".
            "\t$"."this->success('添加成功！', U('list'));\n".
            "};\n";
        $result=array('code'=>0,'msg'=>'ok','codes'=>$codes); 
        $this->ajaxReturn($result);
    }
    
    /*
     * 返回CURD EDIT 代码
     */
    function getCURDEditCode($tablename="",$fieldListstr=""){
        $fieldArray=explode(',',$fieldListstr);
         $reqstr="\n";
        for($i=0;$i<count($fieldArray);$i++){
            $reqstr= $reqstr."\t'$fieldArray[$i]'=>$$fieldArray[$i],\n";
        };
        $reqstr=rtrim($reqstr, ",");
        
        $codes="\n$$tablename = array($reqstr); \n". 
            " if (!M('$tablename')->where(array('id' => $"."id))->save($$tablename)) {\n".
            "\t$"."this->error('修改失败！');\n".
            "} else {\n".
            "\t$"."this->success('修改成功！', U('list'));\n".
            "};\n";
        $result=array('code'=>0,'msg'=>'ok','codes'=>$codes); 
        $this->ajaxReturn($result);
    }
    
    /*
     * 返回CURD List 代码
     */
    function getCURDListCode($tablename="",$fieldListstr=""){
        
        $codes="\n".
        "$"."name = I('name');\n".
        "$"."map['name'] = array('like', '%' . (string) $"."name . '%');\n".
        "$"."list = $"."this->lists('$tablename', $"."map);\n".
        "int_to_string($"."list);\n".
        "$"."this->assign('_list', $"."list);\n".
        "$"."this->meta_title = '标题';\n".
        "$"."this->display();\n";
        $result=array('code'=>0,'msg'=>'ok','codes'=>$codes); 
        $this->ajaxReturn($result);
    }
    
    /*
     * 返回CURD Del 代码
     */
    function getCURDDelCode($tablename="",$fieldListstr=""){
        $codes ="\n".
       "/**\n".
       "* 删除方法\n".
       "* @author 曹梦龙 <138888611@qq.com>\n".
       "*/\n".
        "public function del($"."id = ''){\n".
       "     if(!is_null($"."id)){       \n".     
       "         if (!M('$tablename')->where(array('id' => $"."id))->delete()){\n".
        "            $"."this->error('删除失败！');\n".
        "        } else {\n".
        "            $"."this->success('删除成功！', U('SpPcategory/index'));\n".
        "        };\n".
        "    }\n".
        "}\n";
         
                
        $result=array('code'=>0,'msg'=>'ok','codes'=>$codes); 
        $this->ajaxReturn($result);
    }
    
    /*
     * 返回changeStatus代码
     */
     function getChangeStatusCode($tablename="",$fieldListstr=""){
        $codes="\n". 
        "/** \n".
        "* 状态修改\n".
        "* @author 曹梦龙 <138888611@qq.com>\n".
        "*/\n".
       "public function changeStatus($"."method = null, $"."id) {\n".
       "   $"."id = is_array($"."id) ? implode(',', $"."id) : $"."id;\n".
        "   if (empty($"."id)) {\n".
        "       $"."this->error('请选择要操作的数据!');\n".
        "   }\n".
       "   $"."map['id'] = array('in', $"."id); \n" .
        "   switch (strtolower($"."method)) {\n".
       "       case 'forbid':\n".
        "           $"."this->forbid('$tablename', $"."map);\n".
        "           break;\n".
        "       case 'resume':\n".
        "           $"."this->resume('$tablename', $"."map);\n".
        "           break;\n".
        "       case 'delete':\n".
        "           $"."this->delete('$tablename', $"."map);\n".
        "           break;\n".
        "       default:\n".
        "           $"."this->error('$tablename');\n".
        "   }\n".
        "}\n";         
        $result=array('code'=>0,'msg'=>'ok','codes'=>$codes); 
        $this->ajaxReturn($result);
     }
}

